//-----------------------------------------------------------------------
// <copyright file="tblParameterDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblParameterDAOBase
    {
        #region Common methods
        public virtual tblParameter CreatetblParameterFromReader(IDataReader reader)
        {
            tblParameter item = new tblParameter();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (int)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"ParameterName")) item.ParameterName = (string)reader["ParameterName"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblParameterFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblParameter methods
            
        public virtual void CreatetblParameter(tblParameter tblParameter)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblParameterInsert");
                
                database.AddInParameter(dbCommand, "@ParameterName", DbType.AnsiString, tblParameter.ParameterName);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblParameter.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblParameterException, ex);
            }
        }

        #endregion

        #region UpdatetblParameter methods
        
        public virtual void UpdatetblParameter(tblParameter tblParameter)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblParameterUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, tblParameter.ID);
                database.AddInParameter(dbCommand, "@ParameterName", DbType.AnsiString, tblParameter.ParameterName);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblParameterException, ex);
            }
        }
        
        #endregion

        #region DeletetblParameter methods
        public virtual void DeletetblParameter(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblParameterDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblParameterException, ex);
            }
        }

        #endregion

        #region GettblParameter methods
        
        public virtual tblParameter GettblParameter(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblParameterGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                tblParameter tblParameter =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblParameter = CreatetblParameterFromReader(reader);
                        reader.Close();
                    }
                }
                return tblParameter;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblParameterException, ex);
            }
        }
        
        #endregion

        #region GettblParameterList methods
        public virtual tblParameterCollection GettblParameterList(tblParameterColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblParameterGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblParameterCollection tblParameterCollection = new tblParameterCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblParameter tblParameter = CreatetblParameterFromReader(reader);
                        tblParameterCollection.Add(tblParameter);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblParameterCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblParameterListException, ex);
            }
        }
        
        public virtual tblParameterCollection GettblParameterList(tblParameterColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblParameterList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}